<!doctype html>
<html>
    <head>
        <!-- meta -->
        <!-- meta -->
        <title>Local data operations</title>
        <!-- css -->
        <link href="../styles/examples.css" rel="stylesheet"/>
        <link href="../../source/styles/nova.common.css" rel="stylesheet"/>
        <link href="../../source/styles/nova.css" rel="stylesheet"/>
        <!-- css -->
        <!-- script -->
        <script src="../js/jquery.js"></script>
        <script src="../../source/js/nova.core.js"></script>
        <script src="../../source/js/nova.data.js"></script>
        <script src="../../source/js/nova.popup.js"></script>
        <script src="../../source/js/nova.list.js"></script>
        <script src="../../source/js/nova.dropdownlist.js"></script>
        <script src="../js/people.js"></script>
        <!-- script -->
    </head>
    <body>
        <!-- nav -->
        <!-- nav -->
        <!-- description -->
        <div class="description">Local data operations</div>
        <!-- description -->
        <div id="example" class="n-content">

            <div class="configuration">
                <span class="configHead">Configure</span>
                <ul class="options">
                    <li>
                        <label for="page">Page:</label>
                        <input type="number" value="1" id="page" min="1" />
                    </li>
                    <li>
                        <label for="order">Order by:</label>
                        <select id="order" style="width:115px;float:right;">
                            <option value="Id">Id</option>
                            <option value="FirstName">First Name</option>
                            <option value="LastName">Last Name</option>
                            <option value="Age">Age</option>
                        </select>
                    </li>
                    <li>
                        <label for="dir">Direction:</label>
                        <select id="dir" style="width:115px;float:right;">
                            <option value="asc">Ascending</option>
                            <option value="desc">Descending</option>
                        </select>
                    </li>
                    <li>
                        <button class="n-button" id="apply">Apply</button>
                    </li>
                </ul>
            </div>

            <table id="people">
                <thead>
                    <th>Id</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Title</th>
                    <th>Age</th>
                </thead>
                <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>

            <script id="template" type="text/x-nova-template">
                <tr>
                    <td>#= Id #</td>
                    <td>#= FirstName #</td>
                    <td>#= LastName #</td>
                    <td>#= Title #</td>
                    <td>#= Age #</td>
                </tr>
            </script>

            <script>
                $(document).ready(function() {
                    // create a template using the above definition
                    var template = nova.template($("#template").html());

                    var dataSource = new nova.data.DataSource({
                        pageSize: 10,
                        data: createRandomData(500),
                        change: function() { // subscribe to the CHANGE event of the data source
                            // update the max attribute of the "page" input
                            $("#page").attr("max", this.totalPages());

                            $("#people tbody").html(nova.render(template, this.view()));
                        }
                    });

                    // read the data
                    dataSource.read();

                    $("#apply").click(function() {
                        var page = parseInt($("#page").val(), 10);

                        // validate the page - it must be a number within the allowed range
                        if (isNaN(page) || page < 1 || page > dataSource.totalPages()) {
                            alert(nova.format("Page must be a number between 1 and {0}", dataSource.totalPages()));
                            return;
                        }

                        // query the remote service
                        dataSource.query({
                            page: page,
                            pageSize: 10,
                            sort: {
                                field: $("#order").val(),
                                dir:$("#dir").val()
                            }
                        });
                    });

                    //initialize dropdownlist components
                    $("#order").novaDropDownList();
                    $("#dir").novaDropDownList();
                });
            </script>

            <style scoped>
                #people {
                    border-collapse: separate;
                    border-spacing: 0;
                }

                #people th {
                    background-color: #ef652a;
                    padding: 9px 6px;
                    color: #fff;
                    text-align: left;
                    line-height: 14px;
                    border-width: 1px;
                    border-style: solid;
                    border-color: #d75b26 #d75b26 #d75b26 #f28455;
                }

                #people td {
                    line-height: 23px;
                    padding: 0 6px;
                    background: #eaeaea;
                    color: #3d3d3d;
                    border-width: 1px;
                    border-style: solid;
                    border-color: #f4f4f4 #d6d6d6 #d6d6d6 #f4f4f4;
                }
            </style>
        </div>
        <!-- tools -->
        <!-- tools -->
    </body>
</html>
